enum MusicChangeType {
  progress,
  playing
}
class MusicModel {
  String title;// 歌曲标题
  String author;// 作者
  String url;// 音频地址
  int progress;// 播放进度
  int length;// 歌曲时长，单位秒
  bool playing;// 是否正在播放
  List<Lyric> lyrics;// 歌词列表
  MusicModel({
    this.title,
    this.author,
    this.url,
    this.progress,
    this.length,
    this.playing,
    this.lyrics
  });  

  static String lengthToString(int length) {
    return _padString((length/60).floor()) + ':' + _padString(length%60);
  }

  static String _padString (int val) {
    return val > 9 ? val.toString() : '0' + val.toString();
  }

  ///获取当前激活歌词索引
  int getActiveLyricIndex () {
    double progress = this.progress/100*this.length*1000;// 当前歌曲播放到的时间，毫秒
    return this._judgeCurLyricIndex(this.lyrics, 0, this.lyrics.length, progress);
  }
  int _judgeCurLyricIndex(arr,int start,int end,playTime){//判断当前应该高亮的歌词的数组索引
    if(end >= arr.length ) end = arr.length - 1;
    if(start == end){
        return start;
    }
    else{
        var mid = start + ((end - start)/2).floor();
        var activeTime = arr[mid].activeTime * 1000;
        if(playTime == activeTime){
            return mid;
        }
        else if(playTime < activeTime){
            if(mid == 0){
                return mid;
            }
            else if(playTime >= arr[mid-1].activeTime * 1000){
                return mid - 1;
            }
            else{
                return this._judgeCurLyricIndex(arr,start,mid-2,playTime);
            }
        }
        else{
            if(mid == arr.length - 1){
                return mid;
            }
            else if(playTime < arr[mid+1].activeTime * 1000){
                return mid;
            }
            else{
                return this._judgeCurLyricIndex(arr,mid+1,end,playTime);
            }
        }
    }
}
}

// 歌词句子
class Lyric {
  int activeTime;// 歌词高亮的时间，单位秒
  String content;// 歌词内容
  Lyric({
    this.activeTime,
    this.content
  });
}